package com.bbs.server;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
//import org.junit.Test;

import com.alibaba.fastjson.JSON;
import com.bbs.beans.CommitBack;
import com.bbs.beans.Submit;

import manager.PersistentManager;
import manager.Utils;

/**
 * 给帖子发表回复 Servlet implementation class SendCommitBack
 */
// @WebServlet("/SendCommitBack")
public class SendCommitBack extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public SendCommitBack() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// response.getWriter().append("Served at:
		// ").append(request.getContextPath());
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

		SessionFactory sf = PersistentManager.sharemanager().getSessionFactory();
		Session session = sf.openSession();

		String data = request.getParameter("data");
		Upload upload = JSON.parseObject(data, Upload.class);

		Download download = new Download();

		Transaction tx = session.beginTransaction();

		try {
			
			Submit submit = upload.getCommitBack().getSubmit();
			Submit submitReal = (Submit) session.get(Submit.class, submit.getId());
			submitReal.setCommitbackcount(submitReal.getCommitbackcount()+1);
//			session.save(submitReal);
			submitReal.setChangeTime(Utils.currentTime());
			session.save(upload.getCommitBack());
			tx.commit();
			download.setStatus(DownloadInfo.SUCCESS);
		} catch (Exception e) {
			download.setStatus(DownloadInfo.FAILED);
			tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}

		System.out.println(JSON.toJSONString(download));
		response.getWriter().append(JSON.toJSONString(download));
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

	// 上行数据
	public static class Upload {
		// 回复
		CommitBack commitBack;

		public CommitBack getCommitBack() {
			return commitBack;
		}

		public void setCommitBack(CommitBack commitBack) {
			this.commitBack = commitBack;
		}

	}

	// 下行数据
	public static class Download {
		// 状态
		int status;

		public int getStatus() {
			return status;
		}

		public void setStatus(int status) {
			this.status = status;
		}

	}

	/*@Test
	public void test1() {
		Upload upload = new Upload();
		CommitBack commitBack = new CommitBack();
		upload.setCommitBack(commitBack);
		commitBack.setContentBack("哈哈哈哈好");
		User from = new User();
		from.setId(6);
		commitBack.setFrom(from);
		Submit submit = new Submit();
		submit.setId(2);
		commitBack.setSubmit(submit);
		commitBack.setTime(Calendar.getInstance().toString());
		User to = new User();
		to.setId(7);
		commitBack.setTo(to);

		System.out.println(JSON.toJSONString(upload));
	}*/

}
